import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpEchoServce {
DatagramSocket socket = null;

    public UdpEchoServce(int port) throws SocketException {
        this.socket = new DatagramSocket(port);
    }
    public void start() throws IOException {
        System.out.println("服务端开始");
        while(true){
            DatagramPacket requesPacket = new DatagramPacket(new byte[4096],4096);
            socket.receive(requesPacket);
            String request = new String(requesPacket.getData(),0,requesPacket.getLength());
            String respone = pocess(request);
            DatagramPacket resspondPacket = new DatagramPacket(respone.getBytes(),respone.getBytes().length,requesPacket.getSocketAddress());
            socket.send(resspondPacket);
            System.out.println(requesPacket.getAddress().toString()+" "+requesPacket.getPort()+" "+request+" "+respone);
        }
    }

    private String pocess(String request) {
        return  request;
    }

    public static void main(String[] args) throws IOException {
        UdpEchoServce udpEchoServce  = new UdpEchoServce(2456);
        udpEchoServce.start();
    }
}
